//
//  RootViewController.m
//  View_1
//
//  Created by 愤怒的振振 on 15-4-15.
//  Copyright (c) 2015年 LiuWeiZhen. All rights reserved.
//

#import "RootViewController.h"
#import "UIButton+util.h"
#import "UILabel+util.h"
#import "StudentView.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // button
    CGRect frame = CGRectMake(20, 30, self.view.frame.size.width - 40, 40);
    [self.view addSubview:[UIButton buttonWithTitle:@"测试Block" frame:frame target:self selector:@selector(testBlock:)]];
    
    // label: 用于显示studentView回传过来的学生姓名
    frame.origin.y = CGRectGetMaxY(frame) + 20;
    UILabel *showLabel = [UILabel labelWithFrame:frame text:@"" bgColor:[UIColor yellowColor]];
    showLabel.tag = 100;
    [self.view addSubview:showLabel];
    
    // 新建StudentView
    frame.origin.y = CGRectGetMaxY(frame) + 20;
    frame.size.height = 300;
    StudentView *studentView = [[StudentView alloc] initWithFrame:frame];
    // void (^StudentViewBlock)(NSString *str);
    __block RootViewController *_self = self; // 以后讲
    
    // 给studentView的blockHandler属性赋值（代码块，函数体）
    // 当点击studentView里面的button的时候，执行函数体：
    // 在studentView里面调用：self.blockHandler(self.name);  // self->studentView
    /**
      如果说用代理实现传值
      1. studentView.delegate = self;
      2. RootViewController <StudentViewDelegate>
      3. RootViewController实现代理方法 - (void)didChangeName:(NSString *)name
      4. 在StudentView里面有一个id <StudentViewDelegat> delegate，点击按钮，告诉delegate, [self.delegate didChangeName:self.name]
     */
    [studentView setBlockHandler:^(NSString *str) {
        NSLog(@"%@", str);
        UILabel *label = (UILabel *)[_self.view viewWithTag:100];
        label.text = str;
    }];
    // studentView.blockHandler = ...
    studentView.tag = 101;
    studentView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:studentView];
    [studentView release];
}

- (void)testBlock:(UIButton *)button {
    
}

@end
